package com.sunmnet.auto.spring.mvc.variable.impl;

import com.sunmnet.auto.spring.mvc.baen.FieldSturcture;
import com.sunmnet.auto.spring.mvc.baen.TBStructure;
import com.sunmnet.auto.spring.mvc.temp.FileType;
import com.sunmnet.auto.spring.mvc.variable.IVarDeal;

public class AddOrEditJspFormCols implements IVarDeal {

	public String excute(FileType type, TBStructure tb) {
		StringBuilder sb = new StringBuilder();
		
		int count = 0;
		for (FieldSturcture field : tb.getFields()) {
			if(field.getPropertyName().trim().equalsIgnoreCase("id")||
					field.getPropertyName().trim().equalsIgnoreCase("createTime")||
					field.getPropertyName().trim().equalsIgnoreCase("modifyTime")){
				continue;
			}
		/*	
			<tr>
				<th style=\"width: 80px;\">账户名称</th>
				<td><input id="username" name="username" class="easyui-validatebox" data-options="required:true"/></td>
				<th style="width: 80px;">中文名称</th>
				<td><input id="nameCn" name="nameCn" class="easyui-validatebox"
						   data-options="required:true"/></td>
			</tr>
		*/
			if(count % 2 == 0){
				sb.append("\t\t\t<tr>\r\n");
			}
			
			getTdStirng(sb, field);
			
			count ++;
			if(count % 2 == 0){
				sb.append("\t\t\t</tr>\r\n");
			}
		}
		
		if(count % 2 != 0){
			sb.append("\t\t\t</tr>\r\n");
		}
		return sb.toString();
	}
	
	private void getTdStirng(StringBuilder sb, FieldSturcture field){
		sb.append("\t\t\t\t<th style=\"width: 80px;\">").append(field.getPropertyName()).append("</th>\r\n");
		if(field.getType().equalsIgnoreCase("String")){
			sb.append("\t\t\t\t<td><input id=\"").append(field.getPropertyName()).append("\" name=\"");
			sb.append(field.getPropertyName()).append("\" class=\"easyui-validatebox\"/></td>\r\n");
		}else if(field.getType().equalsIgnoreCase("Date")){
			sb.append("\t\t\t\t<td><input id=\"").append(field.getPropertyName()).append("\" name=\"");
			sb.append(field.getPropertyName()).append("\" class=\"easyui-validatebox easyui-datebox\" editable=\"false\"></td>\r\n");
		}else {
			sb.append("\t\t\t\t<td><input id=\"").append(field.getPropertyName()).append("\" name=\"").append(field.getPropertyName());
			sb.append("\" class=\"easyui-validatebox\" data-options=\"validType:['length[1,2]', 'integer']\"/></td>\r\n");
		}
	}
}
